package com.wushijia.classload;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class InvokeTest {

  public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    Class<?>[] argTypes = new Class[]{String.class};
    Object[] methodArgs = new Object[]{"houyangyang"};
    InvokeTest invokeTest = new InvokeTest();
    Method method = invokeTest.getClass().getMethod("print", argTypes);
    method.invoke(invokeTest, methodArgs);

    Method method2 = invokeTest.getClass().getDeclaredMethod("print2", argTypes);
    method2.setAccessible(true);
    method2.invoke(invokeTest,methodArgs);
  }

  //必须是public方法
  public void print(String name) {
    System.out.println("hello " + name + " I'm InvokeTest::print method");
  }

  //私有方法使用getDeclaredMethod
  private void print2(String name) {
    System.out.println("hello " + name + " I'm InvokeTest::print2 method");
  }
}
